//
//  WSMMessageViewModel.m
//  WinesMall
//
//  Created by Isaac on 17/9/12.
//  Copyright © 2017年 Isaac. All rights reserved.
//

#import "WSMMessageViewModel.h"
#import "WSMMessageModel.h"
@interface WSMMessageViewModel ()

@property (nonatomic, assign) NSInteger currentPageNum;

@end

@implementation WSMMessageViewModel
- (instancetype)init {
    if (self == [super init]) {
        
    }
    return self;
}


- (void)requestNewSubscribeWithPage:(NSInteger)page completion:(void(^)())completion
{
    NSMutableDictionary * parames = [NSMutableDictionary dictionary];
    parames[@"page"] = @(_currentPageNum);
    [TTServerTool postArrayResult:API_user_message param:parames complete:^(BOOL network, TTPublicArrayResult * result, NSError * error) {
        
        if (network) {
            
            if (result) {
                
                if([result.code isEqualToString:@"0"]){
                    
                    if (self.currentPageNum == 1) {
                        [self.messageList removeAllObjects];
//                        for (NSDictionary *dic in result.data) {
//                            WSMMessageModel *model = [[WSMMessageModel alloc] initWithDictionary:dic error:nil];
//                            CGFloat height = [dic[@"content"] heightWithfont:FONT12 width:SCREEN_WIDTH-LEFTSPACE*2];
//                            model.height = height +10 + 40 ;
//                            [self.messageList addObject:model];
//
////                            self.messageList = [WSMMessageModel mj_objectArrayWithKeyValuesArray:result.data];
//
//                        }
                    }else {
                        
//                         [self.messageList addObjectsFromArray: [WSMMessageModel mj_objectArrayWithKeyValuesArray:result.data]];
                    }
                    for (NSDictionary *dic in result.data) {
                        WSMMessageModel *model = [[WSMMessageModel alloc] init];
                        model.content = dic[@"content"];
                        model.create_time = dic[@"create_time"];
                        CGFloat height = [dic[@"content"] heightWithfont:FONT12 width:SCREEN_WIDTH-LEFTSPACE*2];
                        model.height = height +10 + 40 ;
                        [self.messageList addObject:model];
                        
                    }
                    completion();
                    
                }else{
                    [SVProgressHUD showErrorWithStatus:result.msg];
                    completion();
                }
                
            }else{
                [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                completion();
            }
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"网络错误"];
            //网络链接失败
            completion();
        }
        
    }];

}


- (void)requestAdvertisingSubscribeCompletion:(void(^)(id responseObject))completion
{
    //    [BaseHttpRequest post:APICURREVENTS parameter:nil HttpProgress:^(NSProgress *progress) {
    //    } success:^(id responseObject, BOOL error) {
    //        if (error) {
    //
    //            NSArray * adverArray = responseObject[@"data"];
    //            RACSequence *sequence = [[adverArray valueForKey:@"url"] rac_sequence];
    //            RACSignal *signal =  sequence.signal;
    //            [signal subscribeNext:^(NSString * x) {
    //                [self.advertis addObject:x];
    //                if (adverArray.count == self.advertis.count) completion(self.advertis);
    //            }];
    //
    //        }
    //    } failure:^(NSError *error) {
    //    }];
}

@synthesize updateNewsSignal = _updateNewsSignal;
- (RACSignal *)updateNewsSignal
{
    if (!_updateNewsSignal) {
        @synchronized (self) {
            if (!_updateNewsSignal) {
                @weakify(self)
                _updateNewsSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                    @strongify(self);
                    self.currentPageNum = 1;
                    [self requestNewSubscribeWithPage:self.currentPageNum completion:^{
                        [subscriber sendNext:nil];
                    }];
                    
                    return nil;
                    
                    
                    
                }];
                //                self.newPageNum = 0;
            }
        }
    }
    return _updateNewsSignal;
}

@synthesize updateMoreSignal = _updateMoreSignal;
- (RACSignal *)updateMoreSignal
{
    if (!_updateMoreSignal) {
        @synchronized (self) {
            if (!_updateMoreSignal) {
                @weakify(self)
                _updateMoreSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                    @strongify(self);
                    self.currentPageNum += 1;
                    [self requestNewSubscribeWithPage:self.currentPageNum completion:^{
                        [subscriber sendNext:nil];
                    }];
                    return nil;
                }];
                //                self.newPageNum += 10;
            }
        }
    }
    return _updateMoreSignal;
}

//@synthesize cellClickSubJect = _cellClickSubJect;
//- (RACSubject *)cellClickSubJect {
//    if (!_cellClickSubJect) {
//        @synchronized (self) {
//            if (!_cellClickSubJect) {
//                _cellClickSubJect = [RACSubject subject];
//            }
//        }
//    }
//    return _cellClickSubJect;
//}


- (NSMutableArray *)messageList
{
    if (!_messageList) {
        _messageList = [[NSMutableArray alloc] init];
    }
    return _messageList;
}


@end
